"""
系统配置
"""
import os
from typing import List, Optional
from pydantic import BaseSettings, Field


class Settings(BaseSettings):
    """系统配置类"""
    
    # 应用配置
    APP_NAME: str = "A股指数期权自动交易系统"
    APP_VERSION: str = "1.0.0"
    DEBUG: bool = Field(default=True, env="DEBUG")
    
    # 服务器配置
    HOST: str = Field(default="0.0.0.0", env="HOST")
    PORT: int = Field(default=8000, env="PORT")
    
    # 数据库配置
    DATABASE_URL: str = Field(
        default="sqlite:///./trading_system.db",
        env="DATABASE_URL"
    )
    
    # Redis配置
    REDIS_HOST: str = Field(default="localhost", env="REDIS_HOST")
    REDIS_PORT: int = Field(default=6379, env="REDIS_PORT")
    REDIS_DB: int = Field(default=0, env="REDIS_DB")
    REDIS_PASSWORD: Optional[str] = Field(default=None, env="REDIS_PASSWORD")
    
    # 日志配置
    LOG_LEVEL: str = Field(default="INFO", env="LOG_LEVEL")
    LOG_FILE: str = Field(default="logs/trading.log", env="LOG_FILE")
    LOG_MAX_SIZE: int = Field(default=100 * 1024 * 1024, env="LOG_MAX_SIZE")  # 100MB
    LOG_BACKUP_COUNT: int = Field(default=5, env="LOG_BACKUP_COUNT")
    
    # 交易配置
    DEFAULT_CAPITAL: float = Field(default=100000.0, env="DEFAULT_CAPITAL")
    MAX_POSITION_SIZE: float = Field(default=0.1, env="MAX_POSITION_SIZE")  # 最大仓位
    MAX_DAILY_LOSS: float = Field(default=0.03, env="MAX_DAILY_LOSS")  # 最大日亏损
    
    # 风控配置
    RISK_CHECK_INTERVAL: int = Field(default=60, env="RISK_CHECK_INTERVAL")  # 风控检查间隔(秒)
    MAX_DELTA_LIMIT: float = Field(default=0.5, env="MAX_DELTA_LIMIT")
    MAX_GAMMA_LIMIT: float = Field(default=0.1, env="MAX_GAMMA_LIMIT")
    MAX_VEGA_LIMIT: float = Field(default=0.2, env="MAX_VEGA_LIMIT")
    
    # 券商配置
    BROKER_TYPE: str = Field(default="模拟交易", env="BROKER_TYPE")
    BROKER_ACCOUNT: Optional[str] = Field(default=None, env="BROKER_ACCOUNT")
    BROKER_PASSWORD: Optional[str] = Field(default=None, env="BROKER_PASSWORD")
    
    # 行情配置
    MARKET_DATA_INTERVAL: int = Field(default=15, env="MARKET_DATA_INTERVAL")  # 行情更新间隔(秒)
    SUBSCRIBED_SYMBOLS: List[str] = Field(
        default=["510050.SH", "510300.SH", "510500.SH"],
        env="SUBSCRIBED_SYMBOLS"
    )
    
    # 策略配置
    STRATEGY_UPDATE_INTERVAL: int = Field(default=60, env="STRATEGY_UPDATE_INTERVAL")  # 策略更新间隔(秒)
    BACKTEST_DATA_DAYS: int = Field(default=30, env="BACKTEST_DATA_DAYS")  # 回测数据天数
    
    # 通知配置
    ENABLE_NOTIFICATION: bool = Field(default=False, env="ENABLE_NOTIFICATION")
    WECHAT_WEBHOOK: Optional[str] = Field(default=None, env="WECHAT_WEBHOOK")
    EMAIL_SMTP_SERVER: Optional[str] = Field(default=None, env="EMAIL_SMTP_SERVER")
    EMAIL_SMTP_PORT: int = Field(default=587, env="EMAIL_SMTP_PORT")
    EMAIL_USERNAME: Optional[str] = Field(default=None, env="EMAIL_USERNAME")
    EMAIL_PASSWORD: Optional[str] = Field(default=None, env="EMAIL_PASSWORD")
    
    # 系统配置
    TIMEZONE: str = Field(default="Asia/Shanghai", env="TIMEZONE")
    MARKET_OPEN_TIME: str = Field(default="09:30:00", env="MARKET_OPEN_TIME")
    MARKET_CLOSE_TIME: str = Field(default="15:00:00", env="MARKET_CLOSE_TIME")
    
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"


# 全局配置实例
settings = Settings()
